$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
$.extend($.fn.datagrid.methods, {
	'dialog.create': function(jq,operation){
		jq.each(function(){
			var $grid=$(this);
			var state = $grid.data('datagrid');
			if (operation == undefined) {
				operation = 'create';
			}
			var opts = $.extend(true, {}, $.crudgrid.defaults, state.options);
			var param = opts.dialog[operation].param ? '&' + opts.dialog[operation].param : '';
			var method = opts.dialog[operation].name?opts.dialog[operation].name:'新增';
			var target = opts.dialog.name;
			var href = opts.dialog[operation].href ? opts.dialog[operation].href : opts.dialog.url + '/' + (opts.dialog[operation].url ? opts.dialog[operation].url : 'edit');
			var action = opts.dialog[operation].actionHref ? opts.dialog[operation].actionHref : opts.dialog.url + '/'
					+ (opts.dialog[operation].action ? opts.dialog[operation].action : opts.dialog[operation].url);
			$.dialog.open($.extend({}, {
				title : opts.title?opts.title:method + target,
				href : href + '?action=' + action + param,
				buttons : [ $.dialog.buttons.save, $.dialog.buttons.reset, $.dialog.buttons.close ]
			}, opts.dialog.events, opts.dialog[operation], {
				href : href + '?action=' + action + param,
				url : opts.dialog.url?opts.dialog.url+'/':'',
				grid : $grid
			}));
		});
	},
	'dialog.create.parent' : function(jq, operation) {
		jq.each(function() {
			var $grid = $(this);
			var state = $grid.data('datagrid');
			if (operation == undefined) {
				operation = 'create';
			}
			var opts = $.extend(true, {}, $.crudgrid.defaults, state.options);
			var param = opts.dialog[operation].param ? '&' + opts.dialog[operation].param : '';
			var row = $.fn.datagrid.methods.getSelected($grid);
			var method = opts.dialog[operation].name;
			var target = opts.dialog.name;
			var href = opts.dialog[operation].href ? opts.dialog[operation].href : opts.dialog.url + '/' + opts.dialog[operation].url;
			var action = opts.dialog[operation].actionHref ? opts.dialog[operation].actionHref : opts.dialog.url + '/'
					+ (opts.dialog[operation].action ? opts.dialog[operation].action : opts.dialog[operation].url);
			var idField = opts.dialog[operation].idField ? opts.dialog[operation].idField : 'parent';
			param += row == null ? '' : '&' + idField + '=' + row.id;
			$.dialog.open($.extend({}, {
				title : method + target,
				buttons : [ $.dialog.buttons.save, $.dialog.buttons.reset, $.dialog.buttons.close ]
			}, opts.dialog.events, opts.dialog[operation], {
				href : href + '?action=' + action + param,
				url : opts.dialog.url?opts.dialog.url+'/':'',
				grid : $grid
			}));
		});
	},
	'dialog.update' : function(jq, operation) {
		jq.each(function() {
			var $grid = $(this);
			var state = $grid.data('datagrid');
			if (operation == undefined) {
				operation = 'update';
			}
			var opts = $.extend(true, {}, $.crudgrid.defaults, state.options);
			var param = opts.dialog[operation].param ? '&' + opts.dialog[operation].param : '';
			var row = $.fn.datagrid.methods.getSelected($grid);
			var method = opts.dialog[operation].name?opts.dialog[operation].name:'修改';
			var target = opts.dialog.name;
			var href = opts.dialog[operation].href ? opts.dialog[operation].href : opts.dialog.url +'/'+(opts.dialog[operation].url ? opts.dialog[operation].url : 'edit');
			var action = opts.dialog[operation].actionHref ? opts.dialog[operation].actionHref : opts.dialog.url + '/'
					+ (opts.dialog[operation].action ? opts.dialog[operation].action : opts.dialog[operation].url);
			var idField = opts.dialog[operation].idField ? opts.dialog[operation].idField : 'id';
			if (row == null) {
				$.messager.alert('提示', '请选择需' + method + '的' + target, 'info');
			} else {
				$.dialog.open($.extend({}, {
					title : opts.title?opts.title:method + target,
					buttons : [ $.dialog.buttons.save, $.dialog.buttons.reset, $.dialog.buttons.close ]
				}, opts.dialog.events, opts.dialog[operation], {
					href : href + '?action=' + action + '&' + idField + '=' + row.id + param,
					url : opts.dialog.url?opts.dialog.url+'/':'',
					grid : $grid
				}));
			}
		});
	},
	'dialog.remove' : function(jq, operation) {
		jq.each(function() {
			var $grid = $(this);
			var state = $grid.data('datagrid');
			if (operation == undefined) {
				operation = 'remove';
			}
			var opts = $.extend(true, {}, $.crudgrid.defaults, state.options);
			var param = opts.dialog[operation].param ? '&' + opts.dialog[operation].param : '';
			var row = $.fn.datagrid.methods.getSelected($grid);
			var method = opts.dialog[operation].name ? opts.dialog[operation].name : '删除';
			var target = opts.dialog.name;
			var href = opts.dialog[operation].href ? opts.dialog[operation].href : opts.dialog.url + '/' + (opts.dialog[operation].url?opts.dialog[operation].url:'delete');
			var action = opts.dialog[operation].actionHref ? opts.dialog[operation].actionHref : opts.dialog.url + '/'
					+ (opts.dialog[operation].action ? opts.dialog[operation].action : opts.dialog[operation].url);
			var options = $.extend({}, {
				title : method + target
			}, opts.dialog.events, opts.dialog[operation], {
				grid : $grid
			})
			var title = options.title;
			var msg = opts.dialog[operation].msg ? opts.dialog[operation].msg : '确定' + method + '选中的' + target + '吗?';
			var idField = opts.dialog[operation].idField ? opts.dialog[operation].idField : 'id';
			if (row == null) {
				$.messager.alert('提示', '请选择需要' + method + '的' + target, 'info');
			} else {
				$.messager.confirm(title, msg, function(r) {
					if (r) {
						$.post(href, idField + '=' + row.id + param, function(result) {
							if(result.success){
					    		$.messager.show({title:'提示' , msg: result.msg , icon:'info'});
					    		$.crudgrid.refresh($grid);
					    	}else{
					    		$.message.alert({
					    			title: '提示' ,
					    			msg : result.msg ,
					    			icon : 'error'
					    		});
					    	}
						});
					}
				});
			}
		});
	}

	
});
$.extend({
		'crudgrid' : {
			'getGrid' : function(gridButton) {
				return $(gridButton).parent().closest('div.datagrid-wrap').children('div.datagrid-view').children('table.datagrid-f');
			},
			'isTreeGrid':function(target){
				if(target.data('treegrid')){
					return true;
				}
				return false;
			},
			'refresh' : function(target){
				if(target.data('treegrid')){
					target.treegrid('unselectAll');
					target.treegrid('reload');
				}else{
					target.datagrid('unselectAll');
					target.datagrid('reload',{});
				}
			},
			'reload' : function(target){
				if(target.data('treegrid')){
					target.treegrid('reload');
				}else{
					target.datagrid('reload');
				}
				
			},
			'defaults' : {
				'dialog' : {
					'url' : '',
					'name' : '数据',
					'events' : {
						'onSubmitSuccess' : function(data, options) {
							$.dialog.close();
							var $grid = options.grid;
							$.crudgrid.reload($grid);
							//$.crudgrid.gridMethod($grid, 'reload');
							//$.crudgrid.gridMethod($grid, 'clearSelections');
						}
					},
					'create' : {
						'name' : '添加',
						'url' : 'edit',
						'action' : 'save',
						'width' : 400,
						'height' : 200,
						'idField' : 'parent'
					},
					'update' : {
						'name' : '编辑',
						'url' : 'edit',
						'action' : 'save',
						'width' : 400,
						'height' : 200,
						'idField' : 'id'
					},
					'remove' : {
						'name' : '删除',
						'url' : 'delete',
						'action' : 'delete',
						'msg' : '',
						'idField' : 'id'
					},
					'search' : {
						'name' : '查询',
						'url' : 'search'
					}
				}
			},
			'buttons' : {
				'create' : {
					text : '添加',
					iconCls : 'icon-add',
					handler :  function(){
						/*var $dialog=$('<div id="dialog"/>');
						var $grid=$.crudgrid.getGrid(this);
						$dialog.appendTo($grid.parent());
						var $options=$grid.datagrid('options');
						var $path=$options.dialog&&$options.dialog.path?$options.dialog.path:'';
						var $param=$.extend({
							title:'新增',
							width: 260,
							height:250,
							path: $path,
							href: 'edit',
							modal: true,
							grid: $grid,
							onClose: function(){
								$dialog.remove();
							},
							buttons:[$.dialog.buttons.save,$.dialog.buttons.reset,$.dialog.buttons.close]
						},$options.dialog?$options.dialog.create:{});
						var $href="";
						$href=$options.dialog&&$options.dialog.create&&$options.dialog.create.url?$path+'/'+$options.dialog.create.url:$path+'/edit';
						$href=$options.dialog&&$options.dialog.create&&$options.dialog.create.href?$options.dialog.create.href:$href;
						$param.href=$href;
						$dialog.dialog($param);*/
						var $grid=$.crudgrid.getGrid(this);
						var $row=$grid.datagrid('getSelected');
						if($.crudgrid.isTreeGrid($grid)&&$row){
							$grid.datagrid('dialog.create.parent');
						}else{
							$.crudgrid.getGrid(this).datagrid('dialog.create');
						}
					}
				} ,
				'update':{
					text: '编辑' ,
					iconCls : 'icon-edit',
					handler : function(){
						/*var $grid=$.crudgrid.getGrid(this);
						var row=$grid.datagrid('getSelected');
						if(!row){
							$.messager.alert({
								title: '提示' ,
								msg: '请在表格选中一行，再进行编辑操作..' ,
								icon: 'info'
							}) ;
							return;
						}
						var $dialog=$('<div id="dialog"/>');
						$dialog.appendTo($grid.parent());
						var $options=$grid.datagrid('options');
						var $path=$options.dialog&&$options.dialog.path?$options.dialog.path:'';
						var $param=$.extend({
							title: '编辑' ,
							width: 260,
							path: $path,
							height:250,
							href: 'edit',
							modal: true,
							grid: $grid,
							queryParams: {id:row.id},
							onClose: function(){
								$dialog.remove();
							},
							buttons:[$.dialog.buttons.save,$.dialog.buttons.reset,$.dialog.buttons.close]
						},$options.dialog?$options.dialog.update:{});
						var $href="";
						$href=$options.dialog&&$options.dialog.update&&$options.dialog.update.url?$path+'/'+$options.dialog.update.url:$path+'/edit';
						$href=$options.dialog&&$options.dialog.update&&$options.dialog.update.href?$options.dialog.update.href:$href;
						$param.href=$href;
						$dialog.dialog($param);*/
						$.crudgrid.getGrid(this).datagrid('dialog.update');
					}
				},
				'remove' : {
					text : '删除' ,
					iconCls : 'icon-remove' ,
					handler : function(){
						/*var $grid=$.crudgrid.getGrid(this);
						var row=$grid.datagrid('getSelected');
						var $dialog=$grid.datagrid('options').dialog;
						var $remove=$dialog&&$dialog.remove?$dialog.remove:null;
						if(!row){
							$.messager.alert({
								title: '提示' ,
								msg: '请在表格中选中一行，再进行删除操作..' ,
								icon: 'info'
							}) ;
							return;
						}
						var $href='';
						if($dialog&&$dialog.path){
							$href=$dialog.path;
						}
						$href=$remove&&$remove.url?$href+'/'+$remove.url:$href+'/delete';
						$href=$remove&&$remove.href?$remove.href:$href;
						
						$.messager.confirm('确认对话框', '您想要删除选中的这条数据吗？', function(r){
							if (r){
							    $.post($href,{id:row.id},function(result){
							    	if(result.success){
							    		$.messager.show({title:'提示' , msg: result.msg , icon:'info'});
							    		$.crudgrid.refresh($grid);
							    	}else{
							    		$.message.alert({
							    			title: '提示' ,
							    			msg : result.msg ,
							    			icon : 'error'
							    		});
							    	}
							    });
							}
						});*/
						$.crudgrid.getGrid(this).datagrid('dialog.remove');
					}
				},
				'search':{
					text: '查询' ,
					iconCls: 'icon-search' ,
					handler: function(){
						var $grid=$.crudgrid.getGrid(this);
						var $options=$grid.datagrid('options');
						var $dialog=$("<div>");
						$dialog.appendTo($grid.parent());
						var $options=$grid.datagrid('options');
						var $path=$options.dialog&&$options.dialog.url?$options.dialog.url:'';
						var $param=$.extend({
							title: '查询',
							width: 400,
							height:350,
							href: 'search',
							path: $path,
							grid: $grid,
							queryParams:$options.queryParams,
							onClose: function(){
								$dialog.dialog('destroy');
							},
							buttons:[$.dialog.buttons.search,$.dialog.buttons.clear,$.dialog.buttons.close]
						},$options.dialog?$options.dialog.search:{});
						var $href="";
						$href=$options.dialog&&$options.dialog.path?$options.dialog.path:'';
						$href=$options.dialog&&$options.dialog.search&&$options.dialog.search.url?$href+'/'+$options.dialog.search.url:$path+'/search';
						$href=$options.dialog&&$options.dialog.search&&$options.dialog.search.href?$options.dialog.search.href:$href;
						$param.href=$href;
						$dialog.dialog($param);
					}
	
				},
				'refresh':{
					text: '刷新' ,
					iconCls: 'icon-reload' ,
					handler: function(){
						$grid=$.crudgrid.getGrid(this);
						$.crudgrid.refresh($grid);
					}
				}
			}
	}
});
$.extend({
	dialog:{
		'refresh' : function() {
			$.dialog.me(this).dialog('refresh');
		},
		'me':function(dialogButton){
			return $(dialogButton).closest('.window').find('.window-body');
		} ,
		'getForm' : function(dialogButton) {
			return $(dialogButton == undefined ? this : dialogButton).parent().closest('.window').find('form');
		},
		'getGrid' : function(dialogButton) {
			return $(dialogButton == undefined ? this : dialogButton).parent().closest('.window').find('.datagrid-f');
		},
		'open' : function(opt) {
			var options = $.extend({}, $.dialog.defaults, opt,{
				onClose:function(){
					$dialog.dialog('destroy');
				}
			});
			if (options.center == true) {
				var left = $(document).width() - options.width < 0 ? 0 : ($(document).width() - options.width) / 2;
				var top = $(document).height() - options.height < 0 ? 0 : ($(document).height() - options.height) / 2;
				options = $.extend({}, options, {
					left : left,
					top : top,
					closed : false
				});
				options = $.extend({}, options);
			}
			$grid=options.grid;
			var $dialog=$('<div/>');
			$dialog.appendTo($grid.parent());
			$dialog.dialog(options);
		},
		buttons:{
			'reset' : {
				text : '重置',
				iconCls : 'icon-reload',
				handler : function(){
					var $form=$.dialog.getForm(this);
					$form.form('reset');
				}
			},
			'save' : {
				text : '保存',
				iconCls : 'icon-ok',
				handler : function(){
					var $form=$.dialog.getForm(this);
					var $dialog=$.dialog.me(this);
					var $grid=$dialog.dialog('options').grid;
					var $action=$dialog.dialog('options').action;
					var $path=$dialog.dialog('options').url?$dialog.dialog('options').url+'/':'';
					var fd=new FormData($form[0]);
					if(!$form.form('validate')){
						return ;
					}
					var $url=($action=='save'?$path+'save':$action);
		            $.ajax({  
		                url: $url,  
		                type: "POST",  
		                data: fd,  
		                processData: false,  // 不处理数据  
		                contentType: false,   // 不设置内容类型
		                success:function(result){
		              	  if(result.success){
								$.messager.show({title: '提示' , msg:'数据保存成功'});
								$dialog.dialog('close');
								$.crudgrid.reload($grid);
		              	  }else{
								$.messager.alert({
									title:'提示' ,
									msg: result.msg,
									icon: 'error'
								});
		              	  }
		                }
		              });  
				}
			},
			'close' : {
				text : '关闭',
				iconCls : 'icon-cancel',
				handler : function() {
					$.dialog.me(this).dialog('close');
				}
			},
			'search':{
				text : '查询' ,
				iconCls: 'icon-search',
				handler: function(){
					var $form=$.dialog.getForm(this);
					var $grid=$.dialog.me(this).dialog('options').grid;
					var queryParam=$form.serializeObject();
					$grid.datagrid('load',queryParam);
					$.dialog.me(this).dialog('close');
				}
			},
			'clear':{
				text:'清除条件' ,
				iconCls : 'icon-remove' ,
				handler: function(){
					var $form=$.dialog.getForm(this);
					var $grid=$.dialog.me(this).dialog('options').grid;
					$form.form('clear');
					$grid.datagrid('load',{});
				}
			}
		}
	}
});

$.extend({
	'defaults' : {
		//id : 'dialog',
		title : '新窗口',
		href : null,
		modal : true,
		width : 400,
		height : 300,
		resizable : true,
		buttons : null,
		center : true,
		onBeforeSubmit : function(options) {
		},
		onAfterSubmit : function(options) {
			$.dialog.getButtons(this).find('a.l-btn').linkbutton('disable');
		},
		onSubmitSuccess : function(data, options) {
		},
		tools : [ {
			iconCls : 'icon-reload',
			handler : $.dialog.refresh
		} ],
	}

});

$.extend({
	'tabs' : {
		'panel' : {}
	}
});

$.extend($.tabs.panel, {
	'buttons' : {
		'refresh' : {
			iconCls : 'icon-mini-refresh',
			handler : function() {
			var $li = $(this).parent().parent();
				if($li.length > 0){
					var $tab = $('#tabContainer').tabs('getTab', $li.eq(0).index());
					$tab.panel('refresh');
				}
			}
		}
	}
});

